package org.istt.web;

import org.istt.domain.DocumentType;
import org.istt.service.DocumentTypeManager;

import com.opensymphony.xwork2.ActionContext;

/**
 * @author Huy
 *
 */
public class DocumentTypeAction extends BaseAction<DocumentTypeManager, DocumentType, Long> {
	private DocumentType documentType;
	private Long id;
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 9012754123343759656L;
	
	public void setId(Long id) {
		this.id = id;
	}
	@Override
	public Long getId() {
/*		Long id = NumberUtils.toLong(documentType.getId());
		if (id == 0) return null;
		return id;*/
		return (documentType==null? id : documentType.getId());//id
	}


	@Override
	protected void setSaveMessage() {
        ActionContext.getContext().getSession().put("message", 
                getText("documentType.saved"));
	}

	@Override
	protected void setDeleteMessage() {
        ActionContext.getContext().getSession().put("message", 
                getText("documentType.deleted"));
	}


	@Override
	public String save() {
		if (log.isDebugEnabled()) {
            log.debug("entering 'save' method");
        }
        
        //entity = getEntity();
        ((DocumentTypeManager)mgr).save(documentType);
        
        setSaveMessage();
                
        return SUCCESS;
	}

    
	public void setDocumentType(DocumentType documentType) {
		this.documentType = documentType;
	}

	public DocumentType getDocumentType() {
		if (entity!= null && entity.getId() != null) {
			return entity;
		}		
		return documentType;
	}
	
	public boolean isValid() {
		return mgr.isUnique(new String[]{"name"}, new String[]{documentType.getName()}, documentType.getId());
	}
}
